<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件(分为头部和身体) -->
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <!-- tab导航 -->
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="组织导航" name="first">
              <el-row
                type="flex"
                justify="space-between"
                align="middle"
                class="department-header"
              >
                <!-- 第一列20份 -->
                <el-col :span="20">
                  <i class="el-icon-s-home" />
                  <span class="company">江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <!-- 第二列4份 -->
                <el-col :span="4">
                  <!-- 在容器范围里, 一行再分2列 -->
                  <el-row type="flex" justify="end">
                    <el-col>负责人</el-col>
                    <el-col>
                      <!-- 这个之前在头部导航右侧下拉菜单讲过 -->
                      <el-dropdown>
                        <!-- 下拉菜单文字 -->
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <!-- 下拉项 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item>添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>

              <el-tree
                class="departments-tree"
                :data="treeData"
                :props="defaultProps"
                :default-expand-all="true"
              >
                <!-- 默认插槽  并且使用内部的数据进行自定义模板的渲染
                  scope.data 所以直接key解构赋值变量也是data(对应数据对象)
                 -->
                <template #default="{ data }">
                  <el-row
                    type="flex"
                    justify="space-between"
                    align="middle"
                    style="height: 40px; width: 100%"
                  >
                    <!-- 部门名字 -->
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>

                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.name }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown>
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="add(data)">添加子部门</el-dropdown-item>
                              <el-dropdown-item @click.native="edit(data)">编辑部门</el-dropdown-item>
                              <el-dropdown-item v-if="data && !data.children" @click.native="del(data)">删除部门</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>

                  </el-row>
                </template>
              </el-tree>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-card>
      <departDialog
        ref="departDialog"
        :dialog-visible.sync="showDialog"
        :target-id="theParentId"
        :is-edit="isEdit"
        :origin-list="originList"
        @resetList="resetFn"
      />
    </div>
  </div>
</template>

<script>
import { departmentsListAPI, delDepartmentAPI } from '@/api'
import { transTree } from '@/utils'
import departDialog from './components/departDialog.vue'
export default {
  name: 'Departments',
  components: {
    departDialog
  },
  data() {
    return {
      isEdit: false,
      activeName: 'first', // 被激活的 Tab 标签页
      // 树形控件数据
      treeData: [

      ],
      // 定义结构显示
      defaultProps: {
        // children: 'children',
        label: 'name'
      },
      showDialog: false,
      theParentId: '',
      // 用于弹窗内-校验部门code和name是否重复数据数组
      originList: []

    }
  },
  created() {
    this.getDepartmentsListFn()
  },
  methods: {

    async getDepartmentsListFn() {
      const res = await departmentsListAPI()
      const { data: { depts }} = res
      this.originList = depts.map(item => {
        return {
          id: item.id,
          code: item.code,
          name: item.name,
          pid: item.pid
        }
      })
      const add = transTree(depts, '')
      this.treeData = add
    },

    handleClick(tab, event) {
      console.log(tab, event)
    },

    // 正文部分-右侧的添加子部门
    add(data) {
      if (data) { // 数控件中添加子部门
        this.theParentId = data.id
      } else {
        this.theParentId = '' // 添加一级部门
      }
      this.isEdit = false

      this.showDialog = true
    },
    // 编辑子部分
    edit(data) {
      this.isEdit = true
      this.theParentId = data.id
      this.showDialog = true
    },
    // 删除部分
    // 删除部分
    async del(data) {
    // 显示删除确认消息对话框
      const delRes = await this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      // 如果返回的结果是 cancel 说明用户取消了删除
      if (delRes === 'cancel') return this.$message('您取消了删除')

      // 调用删除接口
      const delDepartRes = await delDepartmentAPI(data.id)
      // 根据状态值, 查看是否删除成功
      if (!delDepartRes.success) return this.$message.error(delDepartRes.message)
      // 删除成功需要给用户进行提示
      this.$message.success(delDepartRes.message)
      // 删除后需要重新获取当前页面数据
      this.getDepartmentsListFn()
    },
    resetFn() {
      // this.showDialog = false
      // 重置编辑状态
      // this.isEdit = false
      this.getDepartmentsListFn()
      console.log(this.$refs.departDialog.$refs.deptForm)
      // this.$refs.dialog.$refs.deptForm.resetFields()
    }

  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}
.departments-tree {
  margin: 12px 0px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
